/**
 * 
 */
package systole.persistence.brokersDB;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import systole.domain.clinicalInformation.Surgery;
import systole.domain.clinicalInformation.SurgeryPatient;
import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.SurgeryByPatientBroker;
import systole.view.messages.ErrorMessages;

/**
 * @author jmj
 *
 */
public class SurgeryByPatientBrokerDB extends BrokerDB implements
        SurgeryByPatientBroker {

    /**
     */
    public SurgeryByPatientBrokerDB()  {
        super();
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.SurgeryByPatientBroker#existSurgeryByPatient(systole.domain.clinicalInformation.Surgery, systole.domain.persons.Patient)
     */
    @Override
    public boolean existSurgeryByPatient(Surgery surgery, Patient patient)
            throws ExceptionDAO {
        try {
            this.logger.logDebug("exist surgery by patient");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<SurgeryPatient> list = currentSession.createCriteria(SurgeryPatient.class).add(Restrictions.and(Restrictions.eq("patient", patient), Restrictions.eq("surgery", surgery))).list();
            this.logger.logDebug("exist surgery by patient successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist surgery by patient, msg: " + e.getMessage());
            throw new ExceptionDAO("No se verificar si ya existe la intervención quirúrgicas del paciente", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.SurgeryByPatientBroker#getAllPatientSurgeries()
     */
    @Override
    public List<SurgeryPatient> getAllPatientSurgeries(Patient patient) throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting all patient surgery");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<SurgeryPatient> list = currentSession.createCriteria(SurgeryPatient.class).add(Restrictions.eq("patient", patient)).list();
            this.logger.logDebug("get patient surgery");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all patient surgery, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las intervención quirúrgicas del paciente", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.SurgeryByPatientBroker#insert(systole.domain.clinicalInformation.SurgeryPatient)
     */
    @Override
    public void insert(SurgeryPatient surgeryPatient) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving patient surgery");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(surgeryPatient);
            this.logger.logDebug("saved patient surgery");
        } catch (HibernateException e) {
            this.logger.logError("error on save patient surgery, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.SurgeryByPatientBroker#update(systole.domain.clinicalInformation.SurgeryPatient)
     */
    @Override
    public void update(SurgeryPatient surgeryPatient) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating patient surgery");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(surgeryPatient);
            this.logger.logDebug("updated patient surgery");
        } catch (HibernateException e) {
            this.logger.logError("error on update patient surgery, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public void delete(SurgeryPatient surgeryPatient) throws ExceptionDAO {
        try {
            this.logger.logDebug("deleting patient surgery");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.delete(surgeryPatient);
            this.logger.logDebug("deleted patient surgery");
        } catch (HibernateException e) {
            this.logger.logError("error on delete patient surgery, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo eliminar", e.fillInStackTrace());
        }
    }

}
